package hlhj.fhp.newslib.activitys

import android.support.v7.widget.GridLayoutManager
import android.util.Log
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.model.Response
import com.tenma.ventures.bean.utils.TMSharedPUtil
import hlhj.fhp.newslib.BaseAty
import hlhj.fhp.newslib.R
import hlhj.fhp.newslib.javabean.BaseBean
import hlhj.fhp.newslib.javabean.ColumBean
import hlhj.fhp.newslib.network.Apis
import hlhj.fhp.newslib.network.CustomAction
import hlhj.fhp.newslib.network.LoadingAction
import hlhj.fhp.newslib.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_choose_aty.*
import rx.Observable
import java.util.ArrayList

class ChooseAty : BaseAty() {
    private lateinit var mineAdp:BaseQuickAdapter<ColumBean.DataBean.DefaultBean,BaseViewHolder>
    private lateinit var addAdp:BaseQuickAdapter<ColumBean.DataBean.AllBean,BaseViewHolder>
    private val mineDatas=ArrayList<ColumBean.DataBean.DefaultBean>()
    private val addDatas=ArrayList<ColumBean.DataBean.AllBean>()
    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        mineAdp.setOnItemClickListener { adapter, view, position ->
            if (mineDatas.size==1){

            }else{
                val allBean = ColumBean.DataBean.AllBean()
                allBean.id=mineDatas[position].id
                allBean.category_name=mineDatas[position].category_name
                addDatas.add(0,allBean)
//            addDatas.add(position,allBean)
//            addAdp.notifyItemInserted(position)
                addAdp.notifyItemInserted(0)
                mineDatas.removeAt(position)
                mineAdp.notifyItemRemoved(position)
            }


        }

        addAdp.setOnItemClickListener { adapter, view, position ->

            val allBean = ColumBean.DataBean.DefaultBean()
            allBean.id=addDatas[position].id
            allBean.category_name=addDatas[position].category_name
            mineDatas.add(0,allBean)
//            mineDatas.add(position,allBean)
            mineAdp.notifyItemInserted(0)
            addDatas.removeAt(position)
            addAdp.notifyItemRemoved(position)
        }

        btSave.setOnClickListener {
            var mineStr=""
            var leftStr=""
            for (i in 0..mineDatas.size-1){
                if (i==0){
                    mineStr=mineDatas[i].id.toString()+","
                }else if (i==mineDatas.size-1){
                    mineStr=mineStr+mineDatas[i].id
                }else{
                    mineStr=mineStr+mineDatas[i].id+","
                }
            }
            for (i in 0..addDatas.size-1){
                if (i==0){
                    leftStr=addDatas[i].id.toString()+","
                }else if (i==addDatas.size-1){
                    leftStr=leftStr+addDatas[i].id
                }else{
                    leftStr=leftStr+addDatas[i].id+","
                }
            }
            if (null==mineStr||mineStr.isEmpty()){
                ToastUtil.toast("至少得保留一个栏目")
            }else{
                Apis.edit(TMSharedPUtil.getTMUser(this@ChooseAty).member_id,mineStr,leftStr)
                        .doOnSubscribe(LoadingAction(this))
                        .subscribe(object :CustomAction<Response<BaseBean>>{
                            override fun onResult(t: Response<BaseBean>) {
                                val body = t.body()
                                if (body.code==1){
                                    setResult(-1)
                                    finish()
                                    ToastUtil.toast("保存成功")
                                }else{
                                    ToastUtil.toast(body.message)
                                }
                            }
                        })
            }


        }
    }

    override fun initView() {
        /*适配器处理*/
        mineAdp=object :BaseQuickAdapter<ColumBean.DataBean.DefaultBean,BaseViewHolder>(R.layout.choose_item,mineDatas){
            override fun convert(helper: BaseViewHolder?, item: ColumBean.DataBean.DefaultBean?) {
                helper?.setText(R.id.itemTittle,item?.category_name)
            }
        }
        addAdp=object :BaseQuickAdapter<ColumBean.DataBean.AllBean,BaseViewHolder>(R.layout.choose_item,addDatas){
            override fun convert(helper: BaseViewHolder?, item: ColumBean.DataBean.AllBean?) {
                helper?.setTextColor(R.id.itemTittle,resources.getColor(R.color.textcolor))
                helper?.setText(R.id.itemTittle,item?.category_name)
            }
        }

        mineRecyclerView.adapter=mineAdp
        mineRecyclerView.layoutManager=GridLayoutManager(this,4)
        addRecyclerView.adapter=addAdp
        addRecyclerView.layoutManager=GridLayoutManager(this,4)
        disPatchData()

    }

    private fun disPatchData() {
        Apis.getColumnList(TMSharedPUtil.getTMUser(this).member_id)
                .doOnSubscribe(LoadingAction(this))
                .subscribe(object :CustomAction<Response<ColumBean>>{
                    override fun onResult(t: Response<ColumBean>) {
                        loadingDialog.dismiss()
                        val bean = t.body()
                        mineDatas.addAll(bean.data.defaultX)
                        addDatas.addAll(bean.data.all)
                        mineAdp.notifyDataSetChanged()
                        addAdp.notifyDataSetChanged()
                    }
                })
    }

    override fun getContentId(): Int {
        return R.layout.activity_choose_aty
    }



}
